<?php
/**
 * This example shows making an SMTP connection with authentication.
 */

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
//date_default_timezone_set('Etc/UTC');

//require_once 'PHPMailer-master/PHPMailerAutoload.php';
require_once "PHPMailer-master/class.smtp.php";
require_once "PHPMailer-master/class.phpmailer.php";
/**
 * Class myMail
 * @param
 */
class myMail
{
    private $mail;
    private $configs=[];
    public function setConfigs(){
        if(isset($this->configs['addAddress']))
            $this->mail->addAddress($this->configs['addAddress']);

        if(isset($this->configs['Body']))
            $this->mail->Body=$this->configs['Body'];

        if(isset($this->configs['Subject']))
            $this->mail->Subject=$this->configs['Subject'];
        if(isset($this->configs['addAttachment']))
            $this->mail->addAttachment($this->configs['addAttachment']);


    }

    /**
     * myMail constructor.
     * @param  $arr addAddress=>收件人 Body=>内容 Subject=>主题
     */
    public  function __construct($arr)
    {
        $this->mail = new PHPMailer;
        $this->configs=$arr;
    }

    public function Start()
    {

//Create a new PHPMailer instance
        
//Tell PHPMailer to use SMTP
        $this->mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
        $this->mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
        $this->mail->Debugoutput = 'html';
//Set the hostname of the mail server
        $this->mail->Host = "smtp.163.com";
//Set the SMTP port number - likely to be 25, 465 or 587
        $this->mail->Port = 25;
        $this->mail->CharSet='UTF-8';
//Whether to use SMTP authentication
        $this->mail->SMTPAuth = true;
//Username to use for SMTP authentication
        $this->mail->Username = "chzhr01";
//Password to use for SMTP authentication
        $this->mail->Password = "`12345";
//Set who the message is to be sent from
        $this->mail->setFrom('chzhr01@163.com', 'First Last');
//Set an alternative reply-to address
        $this->mail->addReplyTo('chzhr01@163.com', 'First Last');
//Set who the message is to be sent to
        $this->mail->addAddress('czrll2@163.com', 'John Doe');

//Set the subject line
        $this->mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//        $this->mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
        $time=date("Y m d , H i s");
$this->mail->Body=<<<test
<div style="width: 300px;height: 100px;background: red">$time</div>
test;
        ;
        $this->mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$this->mail->addAttachment('miku.jpg');

//send the message, check for errors
        $this->setConfigs();

        if (!$this->mail->send()) {
            echo "Mailer Error: " . $this->mail->ErrorInfo;
        } else {
            echo "Message sent!";
        }

    }
}